﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.WindowsAzure.Diagnostics;
using System.Configuration;

namespace AzureGames.Diagnostics.Tracing
{
    /// <summary>
    /// Class allows Azure tracing to occur
    /// </summary>
    public static class Tracer
    {
        private static TraceSource ts = new TraceSource("AzureGames");

        static Tracer()
        {
            //Sets up tracing to that the trace informatino gets written to the Azure WADLogsTable 
            //according to the scheduledTransfer period
            var config = DiagnosticMonitor.GetDefaultInitialConfiguration();
            var scheduledTransferPeriod = int.Parse(ConfigurationManager.AppSettings["ScheduledTransferPeriod"]);
            config.Logs.ScheduledTransferPeriod = System.TimeSpan.FromMinutes(scheduledTransferPeriod);
            //I have set this to verbose so that all captured trace messages get written to tablestorage
            //to control which level get captured change the switch value in the web.config
            config.Logs.ScheduledTransferLogLevelFilter = LogLevel.Verbose;
            DiagnosticMonitor.Start("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString", config);
        }
       
        public static void WriteErrorMessage(string message)
        {
            ts.TraceEvent(TraceEventType.Error, 0, message);
        }

        public static void WriteWarningMessage(string message)
        {
            ts.TraceEvent(TraceEventType.Warning, 0, message);
        }

        public static void WriteInformationMessage(string message)
        {
            ts.TraceEvent(TraceEventType.Information, 0, message);
        }

        public static void WriteVerboseMessage(string message)
        {
            ts.TraceEvent(TraceEventType.Verbose, 0, message);
        }
    }
}
